﻿using B.S.DataBase.MyProject.Domain.Sales;
using B.S.DataBase.MyProject.ErrorCode;
using B.S.DataBase.MyProject.Infrastructure.Interface;
using MediatR;
using T.E.ERP.Write.API.Applicantions.Command.SalesCommand;

namespace T.E.ERP.Write.API.Applicantions.CommandHandler.SalesCommandHandler
{
    /// <summary>
    /// 修改发货通知单状态命令处理
    /// </summary>
    public class UpdateSalesStateCommandHandler : IRequestHandler<UpdateSalesStateCommand, APIResult<int>>
    {
        private readonly IBaseRepository<DeliveryNotice> deliveRep;
        public UpdateSalesStateCommandHandler(IBaseRepository<DeliveryNotice> deliveRep)
        {
            this.deliveRep = deliveRep;
        }
        public Task<APIResult<int>> Handle(UpdateSalesStateCommand request, CancellationToken cancellationToken)
        {
            APIResult<int> result = new APIResult<int>();
            var res = deliveRep.GetAll().Result.Where(x => x.DocumentCode == request.DocumentCode).FirstOrDefault();
            res.DocumentStatus = "审核";
            var model = deliveRep.Update(res).Result;
            if (model>0)
            {
                result.Code = APIEnum.修改成功;
                result.Msg = APIEnum.修改成功.ToString();
                result.data = model;
            }
            else
            {
                result.Code = APIEnum.修改失败;
                result.Msg = APIEnum.修改失败.ToString();
                result.data = model;
            }
            return Task.FromResult(result);
        }
    }
}
